package Demo01;
import java.util.EventListener;
import java.util.EventObject;
import java.util.HashSet;
import java.util.Set;
class YootkEventObject extends EventObject{
    private Object source;
    public YootkEventObject(Object source){
        super(source);
        this.source = source;
    }
    @Override
    public Object getSource(){
        return this.source;
    }
    public void setSource(Object source){
        this.source = source;
    }
}

class YootkEventListener implements EventListener{
    public void fire(YootkEventObject event){
        YootkEventSourceObject sourceObject = (YootkEventSourceObject) event.getSource();
        System.out.println("【数据修改】message = " + sourceObject.getMessage());
    }
}
class YootkEventSourceObject {
    private String message;
    private YootkEventListener[] listeners;
    private int foot = 0;

    public YootkEventSourceObject(String message) {
        this.listeners = new YootkEventListener[5];
        this.message = message;
    }
    public void addListener(YootkEventListener listener){
        if (this.foot < this.listeners.length){
            this.listeners[this.foot++] = listener;
        }
    }
    public void notifyInvoke() {
        for (YootkEventListener listener : this.listeners) {
            if (listener != null) {
                listener.fire(new YootkEventObject(this));
            }
        }
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        if (!this.message.equals(message)) {
            this.message = message;
            this.notifyInvoke();
        }
    }
}

public class ZidingyiDemo01 {
    public static void main(String[] args) throws Exception{
        YootkEventSourceObject object = new YootkEventSourceObject("www.yootk.com");
        object.addListener(new YootkEventListener());
        object.setMessage("沐言科技：www.yootk.com");
    }
}
